package com.h2cloud.threadPool;

import java.util.concurrent.*;

/**
 * @author: wulongkai
 * @create: 2022-09-27 17:32
 **/
public class FutureDemo {
	public static void main(String[] args) {
		// 获取异步数据的实现
		Callable<String> callable = () -> {
			Thread.sleep(3000000);
			return "Hello World";
		};

		// 将异步获取
		FutureTask<String> task = new FutureTask<>(callable);

		// 线程异步获取
		new Thread(task).start();
		// 等待结果线程1
		Thread thread1 = new Thread(() -> {
			try {
				System.out.println(task.get());
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}, "t1");

		// 等待结果线程2
		Thread thread2 = new Thread(() -> {
			try {
				System.out.println(task.get());
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}, "t2");

		thread1.start();
		thread2.start();
	}
}

